package com.hz.friends.application;

import java.io.File;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.auth.GoogleHandle;
import com.androidquery.util.AQUtility;
import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FunctionCallback;
import com.hisun.phone.core.voice.util.Log4Util;
import com.hz.friends.global.Global;
import com.hz.friends.serverrequest.ALUserEngine;
import com.hz.friends.utils.MLog;

public class CustomApplication extends Application {
     public AVUser user = null; //用于页面user信息传递
     public AVObject currgroup = null;//用于页面group信息传递
   
     
 	private static  CustomApplication instance = null;
 	private final String TAG = this.getClass().getSimpleName();
	
	@Override
	public void onCreate() {
		super.onCreate();
     	instance = this;

		AVOSCloud.initialize(this,
				"zw8sz05w7m6ojd9xm5vnhrm0mzgkmqpe0kud43dmf0wl6tez",
				"m8xc1qb4onqx5p8532lqo8l4hetuv7zxdwslxlmma3xl30y9");
		AVOSCloud.useAVCloudCN();
		AVCloud.setProductionMode(false);
		setCache();
	 
	}
	
 	public static CustomApplication getInstance(){
 		return instance;
 	}

 	
	/**
	 * 设置aquery图片缓存路径
	 */
	private void setCache() {
		if (!android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment
				.getExternalStorageState()))
			return;
		String cachepath = Environment.getExternalStorageDirectory()+"/"+Global.productName+"/aquery";
		File cachefiledir = new File(cachepath);
		if (!cachefiledir.exists())
			cachefiledir.mkdirs();
		AQUtility.setCacheDir(cachefiledir);
	}
 	
 	public void showToast(String  text){
 		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
 	}
 	
	/**
	 * User-Agent
	 * 
	 * @return user-agent
	 */
	public String getUser_Agent() {
		String ua = "Android;" 
			+ getOSVersion() + ";" 
			+ com.hisun.phone.core.voice.Build.SDK_VERSION + ";"
			+ com.hisun.phone.core.voice.Build.LIBVERSION.FULL_VERSION + ";"
			+ getVendor() + "-" + getDevice() + ";";
		
		ua = ua + getDevicNO()  + ";" + System.currentTimeMillis() + ";";
		MLog.d(TAG, "User_Agent : " + ua);
		return ua;
	}
	
	
	public String getDevicNO() {
		if(!TextUtils.isEmpty(getDeviceId())) {
			return getDeviceId();
		}
		
		if(!TextUtils.isEmpty(getMacAddress())) {
			return getMacAddress();
		}
		return " ";
	}
	
	public String getDeviceId() {
		TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		if(telephonyManager != null ) {
			return telephonyManager.getDeviceId();
		}
		
		return null;

	}
	
	public String getMacAddress() {
		// start get mac address
		WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		if (wifiMan != null) {
			WifiInfo wifiInf = wifiMan.getConnectionInfo();
			if (wifiInf != null && wifiInf.getMacAddress() != null) {
				// 48位，如FA:34:7C:6D:E4:D7
				return wifiInf.getMacAddress();
			}
		}
		return null;
	}

	/**
	 * device model name, e.g: GT-I9100
	 * 
	 * @return the user_Agent
	 */
	public String getDevice() {
		return Build.MODEL;
	}

	/**
	 * device factory name, e.g: Samsung
	 * 
	 * @return the vENDOR
	 */
	public String getVendor() {
		return Build.BRAND;
	}

	/**
	 * @return the SDK version
	 */
	public int getSDKVersion() {
		return Build.VERSION.SDK_INT;
	}

	/**
	 * @return the OS version
	 */
	public String getOSVersion() {
		return Build.VERSION.RELEASE;
	}

	/**
	 * Retrieves application's version number from the manifest
	 * 
	 * @return versionName
	 */
	public String getVersion() {
		String version = "0.0.0";
		try {
			PackageInfo packageInfo = getPackageManager().getPackageInfo(
					getPackageName(), 0);
			version = packageInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		return version;
	}

	/**
	 * Retrieves application's version code from the manifest
	 * 
	 * @return versionCode
	 */
	public int getVersionCode() {
		int code = 1;
		try {
			PackageInfo packageInfo = getPackageManager().getPackageInfo(
					getPackageName(), 0);
			code = packageInfo.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		return code;
	}
}
